package ofpof.domain.dao;

import java.util.Date;

import ofpof.database.MongoDatabaseConnectionProvider;
import ofpof.domain.Issue;
import ofpof.domain.dao.mapper.IssueMapper;

import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

@Repository
public class IssueDao {
	
	@Autowired
	private MongoDatabaseConnectionProvider connectionProvider;

	@Autowired
	private IssueMapper issueMapper;

	
	public void create(Issue issue) {
		DB connection = connectionProvider.getCurrentConnection();
		DBCollection issueCollection = connection.getCollection("issue");
		issue.setId(new ObjectId());
		issue.setCreatedOn(new Date());
		
		DBObject dbObject = issueMapper.toDbObject(issue);
		issueCollection.insert(dbObject);
	}
	
	public void update(Issue issue) {
		DB connection = connectionProvider.getCurrentConnection();
		DBCollection issueCollection = connection.getCollection("issue");
		BasicDBObject query = new BasicDBObject("_id", issue.getId());
		
		issueCollection.update(query, issueMapper.toDbObject(issue));
	}

}
